<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>分组管理</title>
<meta name="viewport" content="width=100%, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/lib/weix/style/weui.min.css">
<link rel="stylesheet" href="/css/fonts.css">
<link rel="stylesheet" href="/css/comm.css">
<link rel="stylesheet" href="/css/gl_hej.css">
</head>

<body>
<div class="weui-cells__title bg th-xx_top">员工分组<a class="tj J_data" href="javascript:;" data-name="" data-id=""><i class="icon-plus"></i></a></div>
<div class="weui-cells" id="J_list">

</div>

<div class="weui-loadmore" id="J_loadmore">
    <i class="weui-loading"></i>
    <span class="weui-loadmore__tips">正在加载</span>
</div>

<div class="th-tcc__boxs J_tcc_quax">
	<div class="th-tcc__bj J_tcc_quax_clo"></div>
    <div class="th-tcc__box th-tcc__box-bot">
    	<div class="th-tcc__hd-bot th-xx_bot"><span id="J_tcc_name">添加/编辑分组</span><a href="javascript:;" class="clo J_tcc_quax_clo"><i class="icon-close"></i></a></div>
        <div class="th-tcc__bd-bot ft-bot">
        
            <div class="weui-cells weui-cells_form" style="margin-top:0">
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">分组名称</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="text" placeholder="请输分组名称" id="J_name">
                    </div>
                </div>
            </div>
            
            <div style="margin:10px 0 20px; display:block; color:#ccc; font-size:17px; line-height:40px; text-align:center; cursor:pointer" id="J_data_clo">删除</div>
            
        </div>
        
        <div class="th-tcc__ft-bot"><a class="ans" href="javascript:;" id="J_qued" data-id="">完成</a></div>
        
    </div>
</div>

<script src="/js/zepto.min.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script src="/js/comm.js"></script>
<script type="text/javascript">
//分组列表
function fenz_List(id){
	var uid = "${uid}";
	var data = {uid:uid,id:id};   //id 1,员工  2,合伙人
	var url = "/group/group_user";
	
	$('#J_loadmore').show();
	
	$.ajax({
		type: "POST",
		dataType: 'json',
		async: false,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				var list = res.data.list;
				for (var i =0;i<list.length;i++){
					var shtml = '<a class="weui-cell weui-cell_access J_data" href="javascript:;" data-id="'+list[i]['id']+'" data-name="'+list[i]['name']+'">'+
									'<div class="weui-cell__hd ren"><i class="icon-folder"></i></div>'+
									'<div class="weui-cell__bd">'+
										'<p class="cell_tit">'+list[i]['name']+'</p>'
					if(list[i]['id'] == 0){
					shtml +=			'<p class="weui-media-box__desc">系统默认组，不可操作</p>'
					}
					shtml +=		'</div>'
					
					if(list[i]['id'] != 0){
					shtml +=		'<div class="weui-cell__ft"></div>'
					}
					shtml +=	'</a>'
					
					if(list[i]['id'] == 0){
						$('#J_list').prepend(shtml)
					}else{
						$('#J_list').append(shtml)
					}
				}

			}else{
				weui.topTips(res.msg);
			}
		},
		error: function(e){

		},
		complete: function(e){
			$('#J_loadmore').hide();
		}
	});

}

$(function(){	
	//-----------------------------------------------------------------------
	//初始化
	fenz_List(1)
	sessionStorage.setItem("need-refresh", true);  //返回上一页刷新
	//-----------------------------------------------------------------------
	//关闭弹出
	$(document).on('click','.J_tcc_quax_clo',function(){
		$('.J_tcc_quax').removeClass('on')
		$('body').removeClass('fix_box')
	})
	//打开弹出
	var $this_data= []
	$(document).on('click','.J_data',function(){
		var $this_t = $(this)
		var id = $this_t.attr('data-id')
		var name = $this_t.attr('data-name')
		
		$this_data = $this_t;
		
		if(id != ''){					//删除按钮
			$('#J_data_clo').show()
			$('#J_tcc_name').html('编辑分组')
		}else{
			$('#J_data_clo').hide()
			$('#J_tcc_name').html('添加分组')
		}
		
		if(id != 0 || id == ''){		//是否展开
			$('body').addClass('fix_box')
			$('.J_tcc_quax').addClass('on')
			$('#J_name').val(name)
			$('#J_qued').attr('data-id',id)
		}else{
			weui.topTips('默认分组，不可操作！');
		}

	})
	//-----------------------------------------------------------------------
	//删除分组
	$(document).on('click','#J_data_clo',function(){
		
		var id_data = $('#J_qued').attr('data-id')

		weui.dialog({
			title: '删除分组',
			content: '删除后原有分组用户将移至默认分组',
			className: 'custom-classname',
			buttons: [{
				label: '取消',
				type: 'default',
				onClick: function () {}
			}, {
				label: '确定',
				type: 'primary',
				onClick: function () {
					
					var loading = weui.loading('数据提交中', {
						className: 'custom-classname'
					});

					var data = {id:id_data}
					var url = "/group/group_delete"
					
					$.ajax({
						type: "POST",
						dataType: 'json',
						async: false,
						contentType: "application/json; charset=utf-8",
						data: JSON.stringify(data),
						url: url,
						success: function(res){
							if(res.code == 200){
								$('.J_tcc_quax').removeClass('on')
								$('body').removeClass('fix_box')
										
								weui.toast('删除成功', {
									duration: 1500,
									className: 'custom-classname',
									callback: function(){ 
										$('#J_list').html('')
										fenz_List(1)								//提交数据后再次拉去数据
									}
								});
							}else{
								weui.topTips(res.msg);
							}
						},
						error: function(e){
				
						},
						complete: function(e){
							loading.hide();
						}
					});

					 
				}
			}]
		});
			
	})
	//-----------------------------------------------------------------------
	//添加/编辑分组
	$(document).on('click','#J_qued',function(){		
		var name_data = $('#J_name').val().trim()
		var id_qued = $(this).attr('data-id')
		
		if(name_data == ''){
			weui.topTips('请填写正确的名称')
			return false
		}
		
		var data = {}
		var url = "/group/edit_group";;
		
		if(id_qued == ''){										//添加
			data = {pid:1,name:name_data}; 						//pid 1，员工 2，合伙人
		}else{													//编辑
			data = {pid:1,id:id_qued,name:name_data}; 			//pid 1，员工 2，合伙人	
		}
		
		$.ajax({
			type: "POST",
			dataType: 'json',
			async: false,
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(data),
			url: url,
			success: function(res){
				if(res.code == 200){
					
					$('.J_tcc_quax').removeClass('on')
					$('body').removeClass('fix_box')

					weui.toast('操作成功', {
						duration: 1500,
						className: 'custom-classname',
						callback: function(){ 
							$('#J_list').html('')
							fenz_List(1)								//提交数据后再次拉去数据
						}
					});

				}else{
					weui.topTips(res.msg);
				}
			},
			error: function(e){
	
			},
			complete: function(e){
				
			}
		});

	})
	
});
</script>
</body>
</html>